from flask import request, jsonify, Blueprint
from models import Song
from config import static_prefix

bp = Blueprint('song', __name__, url_prefix='/song')


@bp.route('/classic', methods=['GET'])
def song_classic():
    # 返回所有经典歌曲
    res = []
    classics = Song.query.filter_by(is_classic=True).all()
    for i in classics:
        opr = i.opera
        res.append({
            "song_name": i.name,
            "song_name_e": i.name_e,
            "cps_name": opr.cps_name,
            "mp3_src": static_prefix + 'mp3?oprname=' + i.opr_name + '&filename=' + i.mp3_src,
            "opr_name": i.opr_name,
            "pic_src": static_prefix + 'picture?type=opera&filename=' + opr.pic_src,
            "like_cnt": i.like_cnt,
            "score_src": i.score_src
        })
    return jsonify(res)


@bp.route('/opera', methods=['POST'])
def song_opera():
    # 返回一部歌剧的所有歌曲
    data = request.get_json()
    songs = Song.query.filter_by(opr_name=data['opr_name']).all()
    res = []
    for i in songs:
        res.append({
            "like_cnt": i.like_cnt,
            "song_name": i.name,
            "song_name_e": i.name_e,
            "mp3_src": static_prefix + 'mp3?oprname=' + i.opr_name + '&filename=' + i.mp3_src,
            "score_src": i.score_src,
            "is_classic": i.is_classic,
            "act": i.act
        })
    return jsonify(res)
